﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CSSAdriverapp
{
    /// <summary>
    /// Interaction logic for AddVehicle.xaml
    /// </summary>
    public partial class AddVehicle : Window
    {
        public AddVehicle()
        {
            InitializeComponent();
            typeNames = new List<string>();
            DataClassesDataContext db = new DataClassesDataContext();
            IQueryable<VehicleType> types = from c in db.VehicleTypes select c;
            types.ToList();

            foreach (VehicleType c in types)
            {
                typeNames.Add(c.Name);
            }
            vehicleTypeListbox.ItemsSource = typeNames;
        }

        public List<String> typeNames
        {
            get;
            set;
        }

        private void Addvehicle_Click(object sender, RoutedEventArgs e)
        {
            if (vehicleTypeListbox.SelectedItem != null && vehicleNameTextbox.Text != "")
            {
                vehicleAddedSuccessfullyLabel.Visibility = System.Windows.Visibility.Hidden;
                vehicleNameAlreayExists.Visibility = System.Windows.Visibility.Hidden;
                missingFieldsLabel.Visibility = System.Windows.Visibility.Hidden;

                DataClassesDataContext db = new DataClassesDataContext();
                IQueryable<Vehicle> name = from c in db.Vehicles
                                           where c.Name == vehicleNameTextbox.Text
                                           select c;
                name.ToList();

                if (name.Count() == 0)
                {
                    IQueryable<VehicleType> type = from c in db.VehicleTypes
                                                   where c.Name == (String)vehicleTypeListbox.SelectedValue
                                                   select c;
                    type.ToList();

                    // Create a new Order object.
                    Vehicle vehicle = new Vehicle
                    {
                        VehicleTypeID = type.First().ID,
                        Name = vehicleNameTextbox.Text
                    };

                    // Add the new object to the Orders collection.
                    db.Vehicles.InsertOnSubmit(vehicle);

                    // Submit the change to the database.
                    try
                    {
                        db.SubmitChanges();
                        vehicleAddedSuccessfullyLabel.Visibility = System.Windows.Visibility.Visible;
                    }
                    catch (Exception c)
                    {
                        Console.WriteLine(c);
                        // Make some adjustments.
                        // ...
                        // Try again.
                        db.SubmitChanges();
                    }
                }
                else
                {
                    vehicleNameAlreayExists.Visibility = System.Windows.Visibility.Visible;
                }
            }
            else
            {
                missingFieldsLabel.Visibility = System.Windows.Visibility.Visible;
            }
        }
    }
}
